<?php
namespace Home\Model;
use Think\Model;
use Common\Common\Classes\Message;

class ClassStudentService extends Model{
    private $cs_sql;
    public function __construct(){
        $this->cs_sql = D("ClassStudent");
    }
    //---------------------------------获得某个班级申请人(从方法也可以获得申请人数)----------------------------------------------
    public function getApplyStuInfo(){
        $where["class_cla_id"] = $_GET["cad"];
        $where["is_pass"] = 0;
        return $this->cs_sql->relation("student")->where($where)->select();
    }
    //---------------------------------获得某个班级学生(已经通过申请的)----------------------------------------------
    public function getClassStuInfo($np,$field,$is_all){
        $where["class_cla_id"] = $_GET["cad"];
        $where["is_pass"] = 1;
        if($is_all == 0) {
            return $this->cs_sql->relation("student")->where($where)->field($field)->limit(($np - 1) * 20, 20)->select();
        }
        else if($is_all == 1){
            return $this->cs_sql->relation("student")->where($where)->field($field)->select();
        }
    }
    //---------------------------------特殊处理班级学生(去除一些无用数据)----------------------------------------------
    public function unsetStu($students){
        for($i = 0;$i<count($students);$i++){
            unset($students[$i]["class_cla_id"]);
            unset($students[$i]["stu_email"]);
            unset($students[$i]["student_stu_id"]);
        }
        return $students;
    }
    //---------------------------------批准学生加入班级----------------------------------------------
    public function allowStuJoin(){
        $where["class_cla_id"] = $_POST["class_cla_id"];
        $where["student_stu_id"] = array("in",$_POST["student_stu_id"]);
        $update["is_pass"] = 1;
        $successNum = $this->cs_sql->where($where)->save($update);
        if($successNum>0) {
            $cla_sql = new ClassService();
            $num = $cla_sql->updateNum(1,$successNum);
            $data = new Message(1,"学生申请审批成功!",$num);
        }
        else{
            $data = new Message(0,"学生申请审批失败!","");
        }
        return $data;
    }
    //---------------------------------删除学生的申请------------------------------------
    public function removeApply(){
        $where["class_cla_id"] = $_POST["class_cla_id"];
        $where["student_stu_id"] = array("in",$_POST["student_stu_id"]);
        $result = $this->cs_sql->where($where)->delete();
        if($result>0){
            $data = new Message(1,"移除学生申请成功!","");
        }
        else{
            $data = new Message(1,"移除学生申请失败!","");
        }
        return $data;
    }
    //---------------------------------从某一个班级中移除学生---------------------------------
    public function removeStu(){
        $where["class_cla_id"] = $_POST["class_cla_id"];
        $where["student_stu_id"] = array("in",$_POST["student_stu_id"]);
        $successNum = $this->cs_sql->where($where)->delete();
        if($successNum>0){
            $cla_sql = new ClassService();
            $num = $cla_sql->updateNum(0,$successNum);
            $data = new Message(1,"移除学生成功!",$num);
        }
        else{
            $data = new Message(1,"移除学生失败!","");
        }
        return $data;
    }
    //---------------------------------获得某个班级的所有学生(除去本班已有)-----------------------------------
    public function getClassStu(){
        $where["class_cla_id"] = $_POST["class_cla_id"];
        $where["is_pass"] = 1;          //通过的就不现实,还在申请中的仍然显示
        $field = array("student_stu_id");
        $result = $this->cs_sql->where($where)->field($field)->select();
        $hadStu_id = array();
        for($i = 0; $i<count($result);$i++){
            $hadStu_id[$i] = $result[$i]["student_stu_id"];
        }
        if(count($hadStu_id)!=0) {
            $where2["student_stu_id"] = array("not in", $hadStu_id);
        }
        $where2["class_cla_id"] = $_POST["check_cla_id"];
        $where2["is_pass"] = 1;             //已经加入才有效
        return $this->cs_sql->relation("student")->where($where2)->select();
    }
    //---------------------------------为某个班级导入学生------------------------------------
    public function importStu(){
        $count = 0;         //计算成功加入的个数
        for($i=0;$i<count($_POST["stu_id"]);$i++){
            $where["class_cla_id"] = $_POST["class_cla_id"];
            $where["student_stu_id"] = $_POST["stu_id"][$i];
            $check = $this->cs_sql->where($where)->find();          //查看本班级下有没有!
            if($check != ""){           //有的情况下,如果是申请的话就通过!
                if($check["is_pass"] == 0) {
                    $update["is_pass"] = 1;
                    $this->cs_sql->where($where)->save($update);
                    $count++;
                }
                else{
                    continue;      //跳过本次循环
                }
            }
            else{
                $add = $where;
                $add["is_pass"] = 1;
                if($this->cs_sql->create($add,1)) {         //验证一下
                    $result = $this->cs_sql->add($add);     //添加
                    if($result != ""){
                        $count++;
                    }
                }
            }
        }
        if($count != 0){            //有成功的是1
            $cla_sql = new ClassService();
            $cla_sql->updateNum(1,$count);      //有成功才改变班级人数
            $d["success"] = $count;
            $d["error"] = count($_POST["stu_id"]) - $count;
            $data = new Message(1,"导入学生成功!",$d);
        }
        else{                   //1个都不成功就0
            $data = new Message(0,"导入学生失败!","");
        }
        return $data;
    }
    //---------------------------------搜寻学生(关键字搜索)------------------------------------
    public function searchStu(){
        $where["class_cla_id"] = $_POST["class_cla_id"];
        $where["stu_number|stu_name"] = array("like","%".$_POST['keyword']."%");
        return D("ClassStudentView")->where($where)->select();
    }
    //----------------------------------检测是否有权限对班级进行操作----------------------------------------
    public function  checkPower(){
        $where["teacher_tea_id"] = $_SESSION["id"];
        $where["cla_id"] = $_POST["class_cla_id"];
        $result = D("CheckClassView")->where($where)->find();
        if($result != ""){
            $data = new Message(1,"你有权限对该班级进行操作!","");
        }
        else{
            $data = new Message(0,"你没有权限对该班级进行操作!","");
        }
        return $data;
    }
    //----------------------------------以上是老师的操作----------------------------------------
    //----------------------------------以上是老师的操作----------------------------------------
    //----------------------------------以上是老师的操作----------------------------------------

    //----------------------------------以下是学生的操作----------------------------------------
    //----------------------------------以下是学生的操作----------------------------------------
    //----------------------------------获得学生所有所在班级----------------------------------------
    public function getStuAllClass($is_filed,$field){               //获得学生所有的班级(利用is_filed区分获得哪种)
        $where["student_stu_id"] = $_SESSION["id"];
        $where["is_filed"] = $is_filed;             //0表示未归档  1表示归档
        $result = D("ClassStudentView")->where($where)->field($field)->select();
        $data = new Message(1,"获取成功!",$result);
        return $data;
    }


    public function __destruct(){}
}